import Vue from 'vue'
import VueRouter from 'vue-router'




Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: () =>
            import ('../views/Login/login.vue')
    },
    {
        path: '/home',
        redirect: '/welcome',
        meta: {
            //设置auth为true代码页面home页面需要鉴权
            auth: true
        },
        component: () =>
            import ('../views/Home/home.vue'),
        children: [{
                path: '/welcome',
                component: () =>
                    import ('../views/welcome/welcome.vue')
            },
            {
                path: '/users',
                component: () =>
                    import ('../views/users/users.vue')
            },
            {
                path: '/roles',
                component: () =>
                    import ('../views/Roles/roles.vue')
            },
            {
                path: '/rights',
                component: () =>
                    import ('../views/Rights/rights.vue')
            },
            {
                path: '/goods',
                component: () =>
                    import ('../views/Goods/goods.vue'),



            },
            {
                path: '/add',
                component: () =>
                    import ('../views/Goods/add.vue')
            },
            {
                path: '/params',
                component: () =>
                    import ('../views/Params/params.vue')
            },
            {
                path: '/categories',
                component: () =>
                    import ('../views/Categories/categories.vue')
            },
            {
                path: '/orders',
                component: () =>
                    import ('../views/orders/orders.vue')
            },
            {
                path: '/reports',
                component: () =>
                    import ('../views/reports/reports.vue')
            },
        ]
    }
]

const router = new VueRouter({
    routes
})

//设置路由守卫
router.beforeEach((to, from, next) => {
        console.log('to', to);
        const flag = to.matched.some(item => {
            return item.meta.auth
        })
        if (flag) {
            const token = sessionStorage.getItem('token')
            if (!token) {
                next({
                    path: '/login'
                })
            } else {
                next()
            }
        } else {
            next()
        }
    })
    //如果flag为true代表需要鉴权


export default router